package com.example.demo.test;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/test")
@RestController
public class test {

    @RequestMapping("get")
    public String  get() {
        return "hello";
    }

//    1.一个参数
    @RequestMapping(value = "/t1",method = RequestMethod.GET)
    public String t1(String name) {
        return "name:"+name;
    }
//   2.两个参数
    @RequestMapping("/t2")
    public String t2(String name,Integer age) {
        return "name:"+name+",age:"+age;
    }
//    3.传递对象
    @RequestMapping("/t3")
    public String t3(User user) {
        return user.toString();
    }

//    4.对参数进行重命名
    @RequestMapping("/t4")
    public String t4(@RequestParam("name") String username) {
        return "username:"+username;
    }
//    5.设置参数为非必传
    @RequestMapping("/t5")
    public String t5(@RequestParam(value = "name",required = false)String name) {
        return "username:"+name;
    }
//    6.传递数组
    @RequestMapping("t6")
    public String t6(String[] arr) {
        return Arrays.toString(arr);
    }
//    7.传递集合
    @RequestMapping("/t7")
    public String t7(@RequestParam("list")List<String>list) {
        return list.toString()+"size: "+list.size();
    }

//    8.对象转成json格式 把请求内容转换成json
//    注意对象的构造方法 如果有自定义， 记得加上默认构造!!!
    @RequestMapping("/t8")
    public String t8(@RequestBody User user) {
        return user.toString();
    }
//    9.从url中获取参数
    @RequestMapping("/t10/{name}/{age}")
    public String t10(@PathVariable String name, @PathVariable Integer age) {
        return "name:"+name+",age:"+age;
    }
//    10.上传文件
    @RequestMapping("/t11")
    public String t11(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/vi/tmp/" + fileName));
        return "获取上传文件："+file.getOriginalFilename();
    }


}
